package logic

import (
	"context"
	"github.com/pkg/errors"
	"yangming-college/app/usercenter/cmd/v1/rpc/internal/svc"
	"yangming-college/app/usercenter/cmd/v1/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
	return &AuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthLogic) Auth(in *pb.AuthReq) (*pb.AuthResp, error) {
	// todo: add your logic here and delete this line
	if ok, err := l.svcCtx.Enforcer.Enforce(in.Sub, in.Obj, in.Act); err != nil || !ok {
		return &pb.AuthResp{
			Ok: false,
		}, errors.Wrapf(err, "usercenter v1 rpc Auth error 1")
	}
	return &pb.AuthResp{
		Ok: true,
	}, nil
}
